<?php
/**
 * @title 1020. 飞地的数量 - 迭代
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function numEnclaves($grid) {
        /**
         * @since 2022-02-17 行，列总数
         */
        $rowCnt = count($grid);
        $colCnt = count($grid[0]);

        /**
         * @since 2022-02-17 检查陆地
         */
        $nodes = [];

        /**
         * @since 2022-02-17 4条边找陆地
         */
        for($i=0; $i<$colCnt; $i++){
            if($grid[0][$i] == 1){
                $grid[0][$i] = 0;
                $nodes[] = [0, $i];
            } else {}

            if($grid[$rowCnt-1][$i] == 1){
                $grid[$rowCnt-1][$i] = 0;
                $nodes[] = [$rowCnt-1, $i];
            } else {}
        }
        for($j=1; $j<=$rowCnt-2; $j++){
            if($grid[$j][0] == 1){
                $grid[$j][0] = 0;
                $nodes[] = [$j, 0];
            } else {}

            if($grid[$j][$colCnt-1] == 1){
                $grid[$j][$colCnt-1] = 0;
                $nodes[] = [$j, $colCnt-1];
            } else {}
        }

        /**
         * @since 2022-02-17 延伸查找
         */
        while($nodes){
            $nexts = [];

            foreach ($nodes as list($r, $c)){
                if($r>=1 AND $grid[$r-1][$c] == 1){
                    $nexts[] = [$r-1, $c];
                    $grid[$r-1][$c] = 0;
                } else {}
                if($r<=$rowCnt-2 AND $grid[$r+1][$c] == 1){
                    $nexts[] = [$r+1, $c];
                    $grid[$r+1][$c] = 0;
                } else {}
                if($c>=1 AND $grid[$r][$c-1] == 1){
                    $nexts[] = [$r, $c-1];
                    $grid[$r][$c-1] = 0;
                } else {}
                if($c<=$colCnt-2 AND $grid[$r][$c+1] == 1){
                    $nexts[] = [$r, $c+1];
                    $grid[$r][$c+1] = 0;
                } else {}
            }

            /**
             * @since 2022-02-17 下次迭代
             */
            $nodes = $nexts;
        }

        /**
         * @since 2022-02-17 统计
         */
        $num = 0;
        foreach ($grid as $g){
            $num += array_sum($g);
        }

        /**
         * @return
         */
        return $num;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202202/2022.02.12.php
 */
$datas = [
    [[[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]], // 3
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->numEnclaves(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
